<?php

namespace app\api\controller;

use app\common\model\AppStoreModel;
use think\Exception;
use think\facade\Db;
use think\facade\Request;

class AppStore extends Base
{
    protected $name = 'app_store';

    public function appList(int $pageSize = 10, int $type = 0)
    {
        $list = Db::name('app_store');
        if ($type != 0) {
            $list->where('type', '=', $type);
        }
        $ret = $list
            ->order("create_time", "desc")
            ->paginate([
                'query'     => Request::get(),
                'list_rows' => $pageSize,
            ]);
//        printf(Db::name('app_store')->getLastSql());
//        var_dump(Db::name('app_store')->getLastSql());
        return $this->result($ret, 1, '');
    }

    public function search(string $keyword = '')
    {
        if (empty($keyword)) {
            return $this->result(null, 1, '');
        }
        $ret = Db::name('app_store')
            ->where('name|description', 'like', "%$keyword%")
            ->order("create_time", "desc")
            ->select();
        return $this->result($ret, 1, '');
    }

    public function find()
    {
        $package = Request::post('package');
        $findRet = Db::name('app_store')->where('package', $package)->find();
        if ($findRet != null) {
            $this->result("", 1, "find the object");
        } else {
            $this->result("", 0, "can not find the object");
        }
    }

    public function add()
    {
        $data                = [];
        $data['package']     = Request::post('package') ? Request::post('package') : "";
        $data['icon']        = Request::post('icon') ? Request::post('icon') : "";
        $data['name']        = Request::post('name') ? Request::post('name') : "";
        $data['description'] = Request::post('description') ? Request::post('description') : "";
        $data['size']        = Request::post('size') ? Request::post('size') : 0;
        $data['version']     = Request::post('version') ? Request::post('version') : "";
        $data['status']      = Request::post('status') ? Request::post('status') : 0;
        $data['downpath']    = Request::post('downpath') ? Request::post('downpath') : "";
//        var_dump($data);
//        然后打印原生sql

        if (
            empty($data['package']) ||
            empty($data['icon']) ||
            empty($data['name']) ||
            empty($data['size']) ||
            empty($data['version'])) {
            $this->result(null, 0, '添加失败,参数不全');
        } else {
            try {
                $id = Db::name('app_store')->insert($data);
//                $id = Db::name('app_store')->fetchSql()->save($data);
//                $id = Db::name('app_store')->fetchSql()->save($data);
//                var_dump(Db::name('app_store')->getLastSql());
                if ($id) {
                    $this->result(null, 1, "添加成功,$id");
                } else {
                    $this->result(null, 0, "添加失败,$id");
                }
            } catch (Exception $e) {
//                printf("{$e->getCode()},{$e->getMessage()}");
//                $this->result(null, 0, "添加失败:{$e->getMessage()}");
//                $this->result(null, 0, "添加失败:{$e->getCode()}");
                $this->result(null, 0, "添加失败:{$e->getMessage()}");
//                $this->result(null, 0, "添加失败");
            }
        }
    }


}